package no.hib.mod250.chat.client.view;

import java.util.Date;

import no.hib.mod250.chat.client.controller.ChatController;
import no.hib.mod250.chat.client.controller.Controller.Task;
import no.hib.mod250.chat.client.i18.ChatConstants;
import no.hib.mod250.chat.client.i18.ChatMessages;
import no.hib.mod250.chat.client.util.R;
import no.hib.mod250.chat.client.view.View.StatusObserver;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MessageWindowView extends View implements StatusObserver {

	// CLASSES

	/**
	 * Represents the Chat Window header. Contains header text and last updated
	 * label.
	 */
	private class HeaderComposite extends Composite {

		private HorizontalPanel panel = new HorizontalPanel();
		private HTML header = new HTML("<h2>" + chatWindowConstants.header()
				+ "</h2>");
		private Label lastUpdate = new Label("");

		public HeaderComposite() {
			initWidget(panel);

			panel.setStyleName(R.Class.ChatWindow.header);
			lastUpdate.setStyleName(R.Class.ChatWindow.lastUpdate);

			panel.add(header);
			panel.add(lastUpdate);

			panel.setWidth("100%");
			panel.setCellHorizontalAlignment(lastUpdate,
					HasHorizontalAlignment.ALIGN_RIGHT);
		}

		public void doLastUpdated(Date date) {
			lastUpdate.setText(chatWindowMessages.lastUpdate(date));
			lastUpdate.setTitle(chatWindowMessages.lastUpdateTitle(date));
		}

	}

	// /CLASSES

	// VARIABLES

	// ... VIEWS

	private VerticalPanel panel = new VerticalPanel();
	private MessagesView messagesView;
	private MessageView messageView;
	private HeaderComposite header = new HeaderComposite();

	// ... /VIEWS

	private static ChatConstants.ChatWindow chatWindowConstants = GWT
			.create(ChatConstants.ChatWindow.class);
	private static ChatMessages.ChatWindow chatWindowMessages = GWT
			.create(ChatMessages.ChatWindow.class);

	// /VARIABLES

	// CONSTRUCTOR

	public MessageWindowView(ChatController controller) {
		super(controller);

		// Views
		messagesView = new MessagesView(controller);
		messageView = new MessageView(controller);

		// Add controller ass status observer
		controller.addStatusObserver(this);

		// Set composite to wrap panel
		initWidget(panel);

		// Draw the view
		doDraw();
	}

	// /CONSTRUCTOR

	// FUNCTIONS

	// ... DO

	@Override
	protected void doDraw() {
		// Panel
		panel.setWidth("100%");
		panel.setStyleName(R.Class.ChatWindow.chatWindow);

		// Header
		panel.add(header);
		panel.setCellWidth(header, "100%");

		// Messages View
		panel.add(messagesView);
		panel.setCellWidth(messagesView, "100%");
		messagesView.setWidth("100%");

		// Message View
		panel.add(messageView);
		panel.setCellWidth(messageView, "100%");
		messageView.setWidth("100%");
	}

	@Override
	public void onStatusServerCameBack() {

	}

	@Override
	public void onStatusServerWentAway() {

	}

	@Override
	public void onStatusTaskFinished(Task task) {
		if (task instanceof ChatController.ResultTask) {
			header.doLastUpdated(new Date());
		}
	}

	@Override
	public void onStatusTaskStarted(Task task) {
	}

	// ... /DO

	// /FUNCTIONS

}
